package com.wanli.agent.invite;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseFragment;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.event.UpDateNameEvent;
import com.wanli.agent.utils.QRCodeUtil;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.widget.AppTitleBar;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 *
 */
public class InviteFragment extends BaseFragment {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.iv_erweima)
    ImageView ivErweima;
    @BindView(R.id.tv_name)
    TextView tvName;
    private Unbinder unbinder;
    private UserInfoBean userInfoBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_invite, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        registerEventBus();
        return view;
    }

    public void initView() {
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null && userInfoBean.getData() != null) {
            Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(userInfoBean.getData().getInvite_url(), 480, 480);
            ivErweima.setImageBitmap(mBitmap);
            tvName.setText(userInfoBean.getData().getName());
        }
    }

    @Subscribe
    public void onUpDateNameEvent(UpDateNameEvent event) {
        if (userInfoBean != null && userInfoBean.getData() != null) {
            userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
            tvName.setText(userInfoBean.getData().getName());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
